if exists(select name from sysobjects where name = 'sp_SanPhamGetAllByLoaiSanPhamId')
	drop proc sp_SanPhamGetAllByLoaiSanPhamId
go
/*-- =============================================
	Created by: HP
	Created on: 2014-03-16 (dd/mm/yyyy)
	Description: 
	--

	File:$sql-scripts/Proc.sp_SanPhamGetAllByLoaiSanPhamId.sql
*/-- =============================================

CREATE PROCEDURE sp_SanPhamGetAllByLoaiSanPhamId
	@Id INT,
	@PageIndex INT = 1,
	@PageSize INT = 10,
	@RecordCount INT=0 OUTPUT
AS
BEGIN
	select ROW_NUMBER() over(
		order by Id
	) as RowNumber , Id, a.[TenSanPham],		a.[MoTa],		a.[NoiDung],		a.[GiaBan],		a.[GiaKM],		a.[ThuTu],		a.[Image],		a.[Image1],		a.[Image2],		a.[Image3],		a.[Image4],		a.[Keywords],		a.[Description],		a.[Title],		a.[NguoiTao],		a.[NgayTao],		a.[NguoiSua],
		a.[NgaySua],
		a.[TrangThai],
		a.[LoaiSanPhamId] into #Results
	from SanPham a where LoaiSanPhamId=@Id and a.TrangThai<>255
	set @RecordCount=(select COUNT(Id) from #Results)
	select * from #Results  WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1
	drop table #Results
END
GO
